package cafe;

import cafe.Model.Utility.LoadServerConfig;
import java.util.List;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.*;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

public class Main {

    private static final Logger LOG = Log.getLogger(Main.class);
    
    public static void main(String[] args) throws Exception {

//        Resource fileserver_xml = Resource.newSystemResource("cafe/Config/JettyConfig.xml");
//        XmlConfiguration configuration = new XmlConfiguration(fileserver_xml.getInputStream());
//        Server server = (Server) configuration.configure();
        
        LOG.info("Load Server Config");
        
        List<String> config = LoadServerConfig.loadConfigServer();
        
        Server server = new Server(Integer.valueOf(config.get(1)));
        
        LOG.info("Load Config Successfully");
        
        //Default Handler
        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(false);
        resourceHandler.setWelcomeFiles(new String[]{"index.html"});
        resourceHandler.setResourceBase(".");

        LOG.info("Serving " + resourceHandler.getBaseResource());

        //Servlet handler
        ServletHandler indexHandler = new ServletHandler();
        indexHandler.addServletWithMapping("cafe.Controller.IndexServletController", "/index");
        indexHandler.addServletWithMapping("cafe.Controller.CustommerController", "/customer");
        indexHandler.addServletWithMapping("cafe.Controller.CategoryController", "/category");
        indexHandler.addServletWithMapping("cafe.Controller.FoodController", "/food");
        indexHandler.addServletWithMapping("cafe.Controller.BillController", "/bill");
        
        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[]{
            resourceHandler,
            indexHandler
        });
        server.setHandler(handlers);
        LOG.info("Set HandlerList");
        server.start();

        LOG.info("Server Starting...");
        server.join();
    }

}
